package com.example.administrator.gplineview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Scroller;

/**
 * @author chenj
 * @time 2017/4/14
 * @company zhonglu
 */

public class CustomScrollerView extends LinearLayout {
    private Scroller mScroller;
    private int mHideHeight;
    private View mHideView;
    private int mHideWidth;
    private boolean mChildScroll;

    public CustomScrollerView(Context context) {
        this(context,null);
    }

    public CustomScrollerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomScrollerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mScroller = new Scroller(context);
        mHideView = LayoutInflater.from(context).inflate(R.layout.deal_num, null, false);
        addView(mHideView);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChild(mHideView,widthMeasureSpec,heightMeasureSpec);
        mHideHeight = mHideView.getMeasuredHeight();
        mHideWidth = mHideView.getMeasuredWidth();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int currentHeight = 0;
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            if (i == 0){
                child.layout(0, -childHeight, childWidth, 0);
            }else {
                child.layout(0, currentHeight, childWidth, childHeight + currentHeight);
                currentHeight += childHeight;
            }
        }
    }

    private int mLastX;
    private int mLastY;
    private boolean intercept;
    private int mLastTouchX;
    private int mLastTouchY;
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                intercept = false;
                break;
            case MotionEvent.ACTION_MOVE:
                if (!mChildScroll){
                    if (mLastY < ev.getY()){
                        intercept = true;
                    }else {
                        if (getScrollY() < 0){
                            intercept = true;
                        }else {
                            intercept = false;
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                break;
        }
        mLastY = (int) ev.getY();
        mLastX = (int) ev.getX();
        return intercept;
    }

    public void setChildScrollerState(boolean childScroll){
        mChildScroll = childScroll;
    }

    private boolean isUp;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                if (!mScroller.isFinished()){
                    mScroller.abortAnimation();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (mLastTouchY != 0) scrollBy(0, mLastTouchY - (int)(event.getY()));
                mLastTouchX = (int) event.getX();
                if (mLastTouchY - event.getY() < 0){
                    isUp = false;
                }else {
                    isUp = true;
                }
                mLastTouchY = (int) event.getY();
                break;
            case MotionEvent.ACTION_UP:
                mLastTouchY = 0;
                if (isUp){
                    smoothScrollTo(0,0);
                }else {
                    smoothScrollTo(0,-mHideHeight);
                }
                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()){
            scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
            postInvalidate();
        }
    }

    private void smoothScrollTo(int destX,int destY){
        int scrollY = getScrollY();
        int deltaY = destY - scrollY;
        mScroller.startScroll(0,scrollY,0,deltaY,500);
        invalidate();
    }
}
